<template>
  <div>

  </div>
</template>

<script>
import { getFreeToken } from "@/api/user.js";
export default {
  name: "freeDialog",
  data() {
    return {
      loading: "",
    };
  },
  computed: {
    showLoginDialog() {
      return this.$store.state.user.showLoginDialog;
    },
  },
  watch: {
    showLoginDialog: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        if (newValue) {
          this.reqGetFreeToken();
        }
      },
    },
  },
  methods: {
    reqGetFreeToken() {
      this.loading = this.$loading({
        lock: true,
        text: "正在重新加载",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      const params = {};
      //这个页面是从yl1001.com调过来的，所有需要先请求token，再展示组件
      //这个接口无论什么环境，返回的都是正式环境的token
      getFreeToken(params)
        .then((res) => {
          if (res.code == 200) {
            if (res.data) {
              this.freeToken = res.data;
              localStorage.setItem("token", "Bearer " + this.freeToken);
              sessionStorage.setItem("from", "ylIndex");
              this.loading.close();
              this.$message.success("重新连接服务器成功！");
              setTimeout(() => {
                window.location.reload();
              }, 500);
            } else {
            }
          }
        })
        .catch((e) => {
          console.log(e);
          setTimeout(() => {
            this.reqGetFreeToken();
          }, 500);
        })
        .finally(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
</style>